/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright Red Hat Inc. and Hibernate Authors
 */
package org.hibernate.orm.test.schemaupdate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;

import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.BaseUnitTest;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.util.ServiceRegistryUtil;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@JiraKey(value = "HHH-11817")
@RequiresDialect(H2Dialect.class)
@BaseUnitTest
public class SchemaDropToOutputScriptTest {

	private File output;
	private ServiceRegistry serviceRegistry;
	private MetadataImplementor metadata;


	@BeforeEach
	public void setUp() throws Exception {
		output = File.createTempFile( "creation_script", ".sql" );
		output.deleteOnExit();
		List<String> content = Arrays.asList(
				"This is the database creation script generated by Hibernate"
				, "This is the second line"
				, "This is the third line"
				, "This is the fourth line"
				, "This is the fifth line"
				, "This is the sixth line"
				, "This is the seventh line"
				, "This is the eighth line"
				, "This is the ninth line"
		);

		try (BufferedWriter bw = new BufferedWriter( new FileWriter( output ) )) {
			for ( String s : content ) {
				bw.write( s );
				bw.write( System.lineSeparator() );
			}
		}
	}

	private void createServiceRegistryAndMetadata(String append) {
		final StandardServiceRegistryBuilder standardServiceRegistryBuilder = ServiceRegistryUtil.serviceRegistryBuilder()
				.applySetting( Environment.FORMAT_SQL, "false" )
				.applySetting( Environment.JAKARTA_HBM2DDL_SCRIPTS_ACTION, "drop" )
				.applySetting( AvailableSettings.JAKARTA_HBM2DDL_SCRIPTS_DROP_TARGET, output.getAbsolutePath() );

		if ( append != null ) {
			standardServiceRegistryBuilder.applySetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_APPEND, append );
		}

		serviceRegistry = standardServiceRegistryBuilder.build();

		metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
				.addAnnotatedClass( MyEntity.class )
				.addAnnotatedClass( MySecondEntity.class )
				.buildMetadata();
		metadata.orderColumns( false );
		metadata.validate();
	}

	@AfterEach
	public void tearDown() {
		ServiceRegistryBuilder.destroy( serviceRegistry );
	}

	@Test
	public void testAppendModeFalse() throws Exception {
		createServiceRegistryAndMetadata( "false" );
		HashMap properties = new HashMap<>();
		properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() );

		SchemaManagementToolCoordinator.process(
				metadata,
				serviceRegistry,
				properties,
				null
		);
		List<String> commands = Files.readAllLines( output.toPath() );
		assertThat( commands.size(), is( 2 ) );
		assertThat( commands.get( 0 ), containsString( getDropMyEntityTable() ) );
		assertThat( commands.get( 1 ), containsString( getDropMySecondEntityTable() ) );
	}

	@Test
	public void testAppendModeTrue() throws Exception {
		createServiceRegistryAndMetadata( "true" );
		HashMap properties = new HashMap<>();
		properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() );

		SchemaManagementToolCoordinator.process(
				metadata,
				serviceRegistry,
				properties,
				null
		);
		List<String> commands = Files.readAllLines( output.toPath() );
		assertThat( commands.size(), is( 11 ) );
		assertThat( commands.get( 9 ), containsString( getDropMyEntityTable() ) );
		assertThat( commands.get( 10 ), containsString( getDropMySecondEntityTable() ) );
	}

	@Test
	public void testDefaultAppendMode() throws Exception {
		createServiceRegistryAndMetadata( null );
		HashMap properties = new HashMap<>();
		properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() );

		SchemaManagementToolCoordinator.process(
				metadata,
				serviceRegistry,
				properties,
				null
		);
		List<String> commands = Files.readAllLines( output.toPath() );
		assertThat( commands.size(), is( 11 ) );
		assertThat( commands.get( 9 ), containsString( getDropMyEntityTable() ) );
		assertThat( commands.get( 10 ), containsString( getDropMySecondEntityTable() ) );
	}

	public String getDropMyEntityTable() {
		if ( metadata.getDatabase().getDialect().supportsIfExistsBeforeTableName() ) {
			return "drop table if exists MyEntity";
		}
		else {
			return "drop table MyEntity if exists";
		}
	}

	public String getDropMySecondEntityTable() {
		if ( metadata.getDatabase().getDialect().supportsIfExistsBeforeTableName() ) {
			return "drop table if exists MySecondEntity";
		}
		else {
			return "drop table MySecondEntity if exists";
		}
	}

	@Entity(name = "MyEntity")
	public static class MyEntity {
		@Id
		private Long id;

		private String name;
	}

	@Entity(name = "MySecondEntity")
	public static class MySecondEntity {
		@Id
		private Long id;

		private String name;
	}

}
